package com.xzq.erp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.xzq.erp.domain.dto.CategoryDTO;
import com.xzq.erp.domain.po.ProductCategory;
import com.xzq.erp.domain.vo.CategoryTreeVO;
import com.xzq.erp.domain.vo.CategoryVO;
import com.xzq.erp.enums.ResponseCodeEnum;
import com.xzq.erp.exception.BizException;
import com.xzq.erp.mapper.ProductCategoryMapper;
import com.xzq.erp.service.IProductCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分类表 服务实现类
 * </p>
 *
 * @author xzq
 * @since 2025-05-26
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements IProductCategoryService {

    @Override
    public List<CategoryVO> getProductCategoryList() {
        List<ProductCategory> list = lambdaQuery()
                .eq(ProductCategory::getStatus, true)
                .orderByAsc(ProductCategory::getSort)
                .list();
        if(list.isEmpty()){
            return new ArrayList<>();
        }
        List<CategoryVO> categoryVOS = new ArrayList<>(list.size());
        List<ProductCategory> ProductParentCategory = list.stream().filter(productCategory -> productCategory.getParentId() == 0).collect(Collectors.toList());
        for(ProductCategory productCategory : ProductParentCategory){
            List<CategoryVO> children = new ArrayList<>();
            for(ProductCategory subProductCategory : list){
                if(subProductCategory.getParentId().equals(productCategory.getId()) && subProductCategory.getLevel() == 2){
                    children.add(BeanUtil.toBean(subProductCategory, CategoryVO.class));
                }
            }
            CategoryVO categoryVO = BeanUtil.toBean(productCategory, CategoryVO.class);
            categoryVO.setChildren(children);
            categoryVOS.add(categoryVO);
        }
        return categoryVOS;
    }

    @Override
    public List<CategoryTreeVO> getProductCategoryTree() {
        List<ProductCategory> list = lambdaQuery()
                .orderByAsc(ProductCategory::getSort)
                .list();
        if(list.isEmpty()){
            return new ArrayList<>();
        }
        List<CategoryTreeVO> categoryVOS = new ArrayList<>(list.size());
        List<ProductCategory> ProductParentCategory = list.stream().filter(productCategory -> productCategory.getParentId() == 0).collect(Collectors.toList());
        for(ProductCategory productCategory : ProductParentCategory){
            List<CategoryTreeVO> children = new ArrayList<>();
            for(ProductCategory subProductCategory : list){
                if(subProductCategory.getParentId().equals(productCategory.getId()) && subProductCategory.getLevel() == 2){
                    children.add(BeanUtil.toBean(subProductCategory, CategoryTreeVO.class));
                }
            }
            CategoryTreeVO categoryVO = BeanUtil.toBean(productCategory, CategoryTreeVO.class);
            categoryVO.setChildren(children);
            categoryVOS.add(categoryVO);
        }
        return categoryVOS;
    }

    @Override
    public void addProductCategory(CategoryDTO categoryDTO) {
        ProductCategory productCategory = BeanUtil.toBean(categoryDTO, ProductCategory.class);
        save(productCategory);
    }

    @Override
    public void updateProductCategory(CategoryDTO categoryDTO) {
        if(Objects.isNull(categoryDTO.getId())){
            throw new BizException(ResponseCodeEnum.PARAM_NOT_VALID);
        }
        ProductCategory productCategory = BeanUtil.toBean(categoryDTO, ProductCategory.class);
        updateById(productCategory);
    }

    @Override
    public void deleteProductCategory(Long id) {
        Long count = lambdaQuery()
                .eq(ProductCategory::getParentId, id)
                .count();
        if(Objects.isNull(count) || count > 0){
            throw new BizException(ResponseCodeEnum.PRODUCT_CATEGORY_NOT_EMPTY);
        }
        removeById(id);
    }

    @Override
    public void updateProductStatus(Long id, Integer status) {
        lambdaUpdate()
                .eq(ProductCategory::getId, id)
                .set(ProductCategory::getStatus, status)
                .update();
    }
}
